package com.ssy.lingxi.search.controller.pc;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.CommoditySearchRequest;
import com.ssy.lingxi.search.api.model.request.ShopSearchCustomerRequest;
import com.ssy.lingxi.search.api.model.request.ShopSearchRequest;
import com.ssy.lingxi.search.api.model.response.*;
import com.ssy.lingxi.search.service.ICommodityService;
import com.ssy.lingxi.search.service.IStoreService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @menu: 店铺商城
 * 店铺商城类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/8/1
 */
@RestController
@RequestMapping("/search/shop/store")
public class StoreController extends BaseController {

    @Resource
    private ICommodityService commodityService;

    @Resource
    private IStoreService storeService;

    /**
     * 查询商品会员品类树
     * @param storeId 店铺id
     * @return
     */
    @RequestMapping(value = "/getCustomerCategoryTree", method = RequestMethod.GET)
    public Wrapper<List<CategoryTreeBrandResponse>> getCustomerCategoryTree(@RequestParam(value = "storeId") Long storeId) {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(storeService.getCategoryTreeAndBrand(shopId, storeId));
    }

    /**
     * 查询商品属性
     * @param storeId 店铺id
     * @param categoryId 品类id
     * @return
     */
    @RequestMapping(value = "/getCustomerAttributeByCategoryId", method = RequestMethod.GET)
    public Wrapper<List<AttributeResponse>> getCustomerAttributeByCategoryId(@RequestParam(value = "storeId") Long storeId, @RequestParam(value = "categoryId") Long categoryId) {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(storeService.getAttributeByCategoryId(shopId, storeId, categoryId));
    }

    /**
     * 查询商品品牌
     * @param storeId 店铺id
     * @return
     */
    @RequestMapping(value = "/getBrand", method = RequestMethod.GET)
    public Wrapper<List<BrandResponse>> getBrand(@RequestParam(value = "storeId") Long storeId) {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(storeService.getBrand(shopId, storeId));
    }

    /**
     * 查询归属地市
     * @param storeId 店铺id
     * @return
     */
    @RequestMapping(value = "/getArea", method = RequestMethod.GET)
    public Wrapper<List<ProvinceResponse>> getArea(@RequestParam(value = "storeId") Long storeId) {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(storeService.getArea(shopId, storeId));
    }

    /**
     * 查询商品最大价格(积分)
     * @param storeId 店铺id
     * @return
     */
    @RequestMapping(value = "/getCommodityMaxPrice", method = RequestMethod.GET)
    public Wrapper<Double> getCommodityMaxPrice(@RequestParam(value = "storeId", required = false) Long storeId) {
        return Wrapper.success(commodityService.getCommodityMaxPrice(storeId));
    }

    /**
     * 查询商品列表
     * @param shopSearchRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getCommodityList", method = RequestMethod.POST)
    public Wrapper<PageData<CommoditySearchResponse>> searchCommodityList(@RequestBody ShopSearchRequest shopSearchRequest) {
        Long shopId = this.getHeadersShopId();
        Long storeId = shopSearchRequest.getStoreId();
        if(storeId != null && storeId > 0){
            CommoditySearchRequest commoditySearchRequest = this.modelMapper.map(shopSearchRequest, CommoditySearchRequest.class);
            commoditySearchRequest.setShopId(shopId);
            //判断是否已登录
            if(this.isLogin()){
                UserLoginCacheDTO sysUser = this.getSysUser();
                commoditySearchRequest.setLoginMemberId(sysUser.getMemberId());
                commoditySearchRequest.setLoginMemberRoleId(sysUser.getMemberRoleId());
            }
            return Wrapper.success(commodityService.searchCommodityList(commoditySearchRequest, true, false));
        }else{
            throw new BusinessException(ResponseCode.SEARCH_STORE_NOT_EXIST);
        }
    }

    /**
     * 查询商品详情
     * @param commodityId 商品id
     * @return
     */
    @RequestMapping(value = "/getCommodityDetail", method = RequestMethod.GET)
    public Wrapper<CommodityResponse> getCommodityDetail(@RequestParam("commodityId") Long commodityId) {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = null;
        //判断当前用户是否已登录
        if(this.isLogin()){
            sysUser = this.getSysUser();
        }
        return Wrapper.success(commodityService.getCommodityDetail(commodityId, shopId, sysUser));
    }

    /**
     * 通过商品skuId查询商品详情
     * @param commoditySkuId 商品skuId
     * @return
     */
    @RequestMapping(value = "/getCommodityDetailBySkuId", method = RequestMethod.GET)
    public Wrapper<CommodityResponse> getCommodityDetailBySkuId(@RequestParam("commoditySkuId") Long commoditySkuId) {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = null;
        //判断当前用户是否已登录
        if(this.isLogin()){
            sysUser = this.getSysUser();
        }
        return Wrapper.success(commodityService.getCommodityDetailBySkuId(commoditySkuId, shopId, sysUser));
    }

    /**
     * 查询商品列表--代客下单
     * @param shopSearchCustomerRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getCustomerCommodityList", method = RequestMethod.POST)
    public Wrapper<PageData<CommoditySearchResponse>> getCustomerCommodityList(@RequestBody ShopSearchCustomerRequest shopSearchCustomerRequest) {
        Long shopId = this.getHeadersShopId();
        Long storeId = shopSearchCustomerRequest.getStoreId();
        if(storeId != null && storeId > 0){
            CommoditySearchRequest commoditySearchRequest = this.modelMapper.map(shopSearchCustomerRequest, CommoditySearchRequest.class);
            commoditySearchRequest.setShopId(shopId);
            commoditySearchRequest.setLoginMemberId(shopSearchCustomerRequest.getCustomerMemberId());
            commoditySearchRequest.setLoginMemberRoleId(shopSearchCustomerRequest.getCustomerMemberRoleId());
            return Wrapper.success(commodityService.searchCommodityList(commoditySearchRequest, true, false));
        }else{
            throw new BusinessException(ResponseCode.SEARCH_STORE_NOT_EXIST);
        }
    }

    /**
     * 查询商品详情--代客下单
     * @param commodityId 商品id
     * @return
     */
    @RequestMapping(value = "/getCustomerCommodityDetail", method = RequestMethod.GET)
    public Wrapper<CommodityResponse> getCustomerCommodityDetail(@RequestParam("commodityId") Long commodityId, @RequestParam("customerMemberId") Long customerMemberId, @RequestParam("customerMemberRoleId") Long customerMemberRoleId) {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = new UserLoginCacheDTO();
        sysUser.setMemberId(customerMemberId);
        sysUser.setMemberRoleId(customerMemberRoleId);
        return Wrapper.success(commodityService.getCommodityDetail(commodityId, shopId, sysUser));
    }

    /**
     * 通过商品skuId查询商品详情--代客下单
     * @param commoditySkuId 商品skuId
     * @return
     */
    @RequestMapping(value = "/getCustomerCommodityDetailBySkuId", method = RequestMethod.GET)
    public Wrapper<CommodityResponse> getCustomerCommodityDetailBySkuId(@RequestParam("commoditySkuId") Long commoditySkuId, @RequestParam("customerMemberId") Long customerMemberId, @RequestParam("customerMemberRoleId") Long customerMemberRoleId) {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = new UserLoginCacheDTO();
        sysUser.setMemberId(customerMemberId);
        sysUser.setMemberRoleId(customerMemberRoleId);
        return Wrapper.success(commodityService.getCommodityDetailBySkuId(commoditySkuId, shopId, sysUser));
    }
}
